package com.ss.android.article.base.feature.pgc;

import android.os.Looper;
import android.os.Message;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.collection.MaxSizeLinkedHashMap;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.e;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.frameworks.baselib.network.dispatcher.AbsRequest;
import com.ss.android.article.base.feature.subscribe.model.EntryItem;
import com.ss.android.common.callback.SSCallback;
import java.util.ArrayList;
import java.util.Iterator;

public final class GetEntryItemHelper implements WeakHandler.Callback {
    static final class a$a extends AbsRequest {
        private final GetEntryItemHelper d;
        ArrayList a;
        private long b;
        private volatile boolean c;

        protected a$a(GetEntryItemHelper arg3, long arg4, SSCallback arg6) {
            super("GetEntryItemTask", Priority.IMMEDIATE);
            this.d = arg3;
            this.a = new ArrayList();
            this.b = arg4;
            this.a.add(arg6);
        }

        static boolean a(a$a arg1) {
            return arg1.c;
        }

        private synchronized ArrayList a() {
                return this.a;
        }

        public final void run() {
            Object[] v2;
            WeakHandler v1_2;
            EntryItem v0_1 = null;
            int v5 = 4;
            int v9 = 3;
            int v7 = 2;
            super.run();
            if(this.isCanceled()) {
                this.c = true;
                WeakHandler v0 = this.d.b;
                Object[] v1 = new Object[v5];
                v1[0] = Long.valueOf(this.b);
                v1[1] = null;
                v1[v7] = this.a();
                v1[v9] = this;
                v0.obtainMessage(v7, v1).sendToTarget();
                return;
            }

            try {
                v0_1 = android.support.design.a.d(this.b);
            }
            catch(Throwable v1_1) {
                Logger.w("GetEntryItemHelper", "get pgc entryItem exception: " + v1_1);
            }

            if(this.isCanceled()) {
                this.c = true;
                v1_2 = this.d.b;
                v2 = new Object[v5];
                v2[0] = Long.valueOf(this.b);
                v2[1] = v0_1;
                v2[v7] = this.a();
                v2[v9] = this;
                v1_2.obtainMessage(v7, v2).sendToTarget();
            }
            else {
                this.c = true;
                v1_2 = this.d.b;
                v2 = new Object[v5];
                v2[0] = Long.valueOf(this.b);
                v2[1] = v0_1;
                v2[v7] = this.a();
                v2[v9] = this;
                v1_2.obtainMessage(1, v2).sendToTarget();
            }
        }
    }

    public MaxSizeLinkedHashMap a;
    WeakHandler b;
    private static Object c = new byte[0];
    private static GetEntryItemHelper d;
    private e e;

    private GetEntryItemHelper() {
        this.e = new e();
        this.b = new WeakHandler(Looper.getMainLooper(), ((WeakHandler.Callback)this));
        this.a = new MaxSizeLinkedHashMap(30, 10, true);
    }

    public static GetEntryItemHelper a() {
        if(d == null) {
            Object v1 = c;
            synchronized (v1){
                if(d == null) {
                    d = new GetEntryItemHelper();
                }
            }
        }

        return d;
    }

    public final void a(long arg4, SSCallback arg6) {
        if(arg4 > 0) {
            UIUtils.a();
            Object v0 = this.e.a(Long.valueOf(arg4));
            if(v0 != null && !((a$a)v0).isCanceled() && !a$a.a(((a$a)v0))) {
                ArrayList v1 = ((a$a)v0).a;
                synchronized (v1){
                    ((a$a)v0).a.add(arg6);
                }
                Logger.d("GetEntryItemHelper", "queryEntryItem , add old task;  mMediaId:" + arg4);
                return;
            }

            a$a v0_2 = new a$a(this, arg4, arg6);
            this.e.a(Long.valueOf(arg4), v0_2);
            v0_2.start();
            Logger.d("GetEntryItemHelper", "queryEntryItem , add new task;  mMediaId:" + arg4);
        }
    }

    public final void handleMsg(Message message) {
        boolean v0_2;
        StringBuilder v11;
        String v10;
        ArrayList v2;
        Object v1;
        long v8;
        int v6 = 2;
        int v7 = message.what;
        Object v0 = message.obj;
        switch(v7) {
            case 1:
            case 2: {
                try {
                    label_7:
                    v8 = ((Long)((Object[])v0)[0]).longValue();
                    v1 = ((Object[])v0)[1];
                    if(v1 != null) {
                        this.a.put(Long.valueOf(v8), v1);
                    }
                    v2 = (ArrayList) ((Object[])v0)[2];
                    v0 = ((Object[])v0)[3];
                    if(v0 == this.e.a(Long.valueOf(v8))) {
                        this.e.b(Long.valueOf(v8));
                        Logger.d("GetEntryItemHelper", "remove task; id:" + v8);
                    }

                    int v3;
                    if(v7 == v6) {
                        v3 = v6;
                    } else if(!((a$a)v0).isCanceled()) {
                        v3 = 1;
                    } else {
                        v3 = v6;
                    }

                    v10 = "GetEntryItemHelper";
                    v11 = new StringBuilder("got data : ");
                    if(v1 != null) {
                        v0_2 = true;
                    }
                    else {
                        v0_2 = false;
                    }
                    v11 = v11.append(v0_2).append("; action_cancel :");
                    if(v7 == v6) {
                        v0_2 = true;
                    } else {
                        v0_2 = false;
                    }
                    Logger.d(v10, v11.append(v0_2).toString());
                    if(v2 == null) {
                        return;
                    }

                    Iterator v2_1 = v2.iterator();
                    while(v2_1.hasNext()) {
                        v0 = v2_1.next();
                        if(v0 == null) {
                            continue;
                        }

                        ((SSCallback)v0).onCallback(new Object[]{Integer.valueOf(v3), Long.valueOf(v8), v1});
                    }
                }
                catch(Exception v0_1) {
                    Logger.throwExceptionInDebug(((Throwable)v0_1));
                }
            }
        }

        return;
    }
}

